package com.ihighest.app.infox.activity;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.ihighest.app.common.api.Api;
import com.ihighest.app.common.api.ApiCallback;
import com.ihighest.app.common.api.ApiParam;
import com.ihighest.app.common.api.ApiResult;
import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.SharedPreferencesKit;
import com.ihighest.app.common.kit.ToastKit;
import com.ihighest.app.infox.Const;
import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.OnlineVoteDetailAdapter;

public class OnlineVoteDetailActivity extends BaseActivity implements OnClickListener {
	private String id;// 投票id
	private ListView listView;
	public static List<String> chooseList = new ArrayList<String>();
	private String title;
	private String time;
	private TextView voteTitle, voteTime;
	private String voteState, eventId;

	private String isJoin;// 是否参加

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_activity_onlinevote_detail);
		id = getIntent().getStringExtra("id");
		title = getIntent().getStringExtra("title");
		time = AppKit.timeToBeforeStr(getIntent().getLongExtra("time", 0));
		isJoin = getIntent().getStringExtra("isJoin");
		voteState = getIntent().getStringExtra("state");
		initView();
	}

	/**
	 * 绑定控件
	 * 
	 * 
	 * @author lkx Created at 2015-10-27 上午10:22:46
	 */
	private void initView() {
		findViewById(R.id.onlineVoteDetailBack).setOnClickListener(this);
		findViewById(R.id.onlineVoteDetailVoteing).setOnClickListener(this);
		listView = (ListView) findViewById(R.id.voteDetailListView);
		voteTitle = (TextView) findViewById(R.id.voteTitle);
		voteTitle.setText(title);

		// listView.setOnItemSelectedListener(new OnItemSelectedListener() {
		//
		// @Override
		// public void onItemSelected(AdapterView<?> parent, View view, int
		// position, long id) {
		// listView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
		// EditText edt=(EditText) view.findViewById(R.id.myAnswerEditText);
		// edt.s
		// }
		//
		// @Override
		// public void onNothingSelected(AdapterView<?> arg0) {
		// // TODO Auto-generated method stub
		//
		// }
		// });
		initData(id);
	}

	private void initData(String mId) {
		if (isJoin.equals("no")) {
			// 如果投票用户没有参与
			Api.get("vote/details", ApiParam.create().addParam("id", mId), new ApiCallback(OnlineVoteDetailActivity.this) {

				@Override
				public void handleSuccess(ApiResult result) {
					int count = 0;
					for (int i = 0; i < result.getDatas().length(); i++) {
						try {
							count += result.getDatas().getJSONObject(i).getInt("count");
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					listView.setAdapter(new OnlineVoteDetailAdapter(OnlineVoteDetailActivity.this, result.getDatas(), count));

				}

				@Override
				public void handleFailure(Exception e, String msg) {
					// TODO Auto-generated method stub

				}
			}, OnlineVoteDetailActivity.this);
		} else {
			// 如果用户已经参与
			Api.get("vote/view", ApiParam.create().addParam("id", mId), new ApiCallback(OnlineVoteDetailActivity.this) {

				@Override
				public void handleSuccess(ApiResult result) {
					listView.setAdapter(new OnlineVoteDetailAdapter(OnlineVoteDetailActivity.this, result.getDatas(), "join"));
				}

				@Override
				public void handleFailure(Exception e, String msg) {
					// TODO Auto-generated method stub

				}
			}, OnlineVoteDetailActivity.this);
		}

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.onlineVoteDetailBack:
			finish();
			break;

		case R.id.onlineVoteDetailVoteing:

			if (!voteState.equals("Finished")) {
				if (isJoin.equals("no")) {
					// 没有参与
					if (chooseList.size() == 0) {
						ToastKit.showShort(OnlineVoteDetailActivity.this, "请选择投票选项");
					} else if (!TextUtils.isEmpty(SharedPreferencesKit.getString(OnlineVoteDetailActivity.this, Const.SP_KEY_USER_ID))) {

						Api.post("vote/submit", ApiParam.create().addParam("id", id).addParam("answer", listToString(chooseList)),
								new ApiCallback(OnlineVoteDetailActivity.this) {

									@Override
									public void handleSuccess(ApiResult result) {
										ToastKit.showShort(OnlineVoteDetailActivity.this, "投票成功");
										finish();
									}

									@Override
									public void handleFailure(Exception e, String msg) {
										// TODO Auto-generated method stub

									}
								}, OnlineVoteDetailActivity.this);
					} else {
						ToastKit.showShort(OnlineVoteDetailActivity.this, Const.UN_LOGIN_ALERT);
					}
				} else if (isJoin.equals("yes")) {
					ToastKit.showShort(OnlineVoteDetailActivity.this, "您已参与该项投票");
				}
			} else {
				ToastKit.showShort(OnlineVoteDetailActivity.this, "投票已结束");
			}

			break;
		}

	}

	/**
	 * @Description:把list转换为一个用逗号分隔的字符串
	 */
	public static String listToString(List list) {
		StringBuilder sb = new StringBuilder();
		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				if (i < list.size() - 1) {
					sb.append(list.get(i) + ",");
				} else {
					sb.append(list.get(i));
				}
			}
		}
		return sb.toString();
	}
}
